2022-06-17

Outline

  • Brief intro to the elements of shiny
  • How shiny can help make your work more accessible
  • Examples

shiny

  • Framework for making web apps with R code
  • Relies on a shiny server that runs R code behind the scenes (CDS has a shinyapps.io plan!)
  • Means that you can make web apps with little-to-no knowledge of HTML, CSS or Javascript (unless you want to make things highly personalised)
  • Uses reactive programming so that the outputs react the user’s inputs

Making a shiny app

  • The easiest way to make a shiny app is to make a new R project from RStudio
  • The next easiest way is to create a new R script, call is app.R, and create your app starting from a snippet

Components

A shiny app is made up of two parts: the ui that defines how it looks and the server which defines how it works.

library(shiny)
ui <- fluidPage(
  
)
server <- function(input, output, session) {
  
}

shinyApp(ui, server)

Content that is added to the UI displays to the user.

library(shiny)
ui <- fluidPage(
  "Hello, CDS!"
)

server <- function(input, output, session) {
  
}
shinyApp(ui, server)

Outputs which are rendered on the server side can be displayed by the ui.

You have to tell the ui what sort of object is being rendered:

  • plotOutput() ~ renderPlot()
  • tableOutput() ~ renderTable()
  • leafletOutput() ~ renderLeaflet()

Many of these are in {shiny} but some will be in their respective packages ({leaflet} for the last one)

ui <- fluidPage(
  plotOutput("plot")
)
server <- function(input, output, session) {
  output$plot <- renderPlot({
    hist(rnorm(n=20))
  })
}
shinyApp(ui, server)

ui <- fluidPage(
  plotOutput("plot")
)
server <- function(input, output, session) {
  output$plot <- renderPlot({
    hist(rnorm(n=20))
  })
}
shinyApp(ui, server)

Adding controls

ui <- fluidPage(
  numericInput("sample_size", "Select sample size:", value=20),
  plotOutput("plot")
)

server <- function(input, output, session) {
  output$plot <- renderPlot({
    hist(rnorm(n=input$sample_size))
  })
}

shinyApp(ui, server)

There are lots of ways that you can get inputs from your users

funs <- ls("package:shiny")
funs[grepl("input$", tolower(funs)) & !grepl("update", tolower(funs))]
##  [1] "checkboxGroupInput"      "checkboxInput"          
##  [3] "dateInput"               "dateRangeInput"         
##  [5] "fileInput"               "numericInput"           
##  [7] "passwordInput"           "restoreInput"           
##  [9] "selectInput"             "selectizeInput"         
## [11] "sliderInput"             "snapshotPreprocessInput"
## [13] "textAreaInput"           "textInput"              
## [15] "varSelectInput"          "varSelectizeInput"

(Also see {shinyWidgets} for some fun input options)

ui <- fluidPage(
  checkboxGroupInput('checkboxGroupInput', 'checkboxGroup:', choices=LETTERS[1:3], selected="A"),
  splitLayout(dateInput("dateInput", "date:"), fileInput("fileInput", "file:")),
  shinyWidgets::colorSelectorInput("colorSelectorInput", "colorSelector:", c("red", "green", "blue"), "red"),
  shinyWidgets::downloadBttn("downloadBttn")
)
server <- function(input, output, session) {
}
shinyApp(ui, server)

shiny apps can make your work more accessible

  • For new methods and models, R packages can work well for regular useRs but not necessarily others
  • Some results are complex and hard to show all the possible way data could be visusalised in a single paper

shiny maps can make your work more accessible

{circacompare}

App that allows users to upload their data and fit a nonlinear model (using nls()) that estimates differences in circadian characteristics between two groups.

https://rwparsons.shinyapps.io/circacompare/

Most use the R package but sometimes, they will say that they specifically used the shiny app.Liu, Xianhui, et al. “Hexosamine biosynthetic pathway integrates circadian and metabolic signals to regulate daily rhythms in protein O-linked N-acetylglucosaminylation.” bioRxiv (2020).

shiny maps can make your work more accessible

ECOHIS to CHU9D mapping

  • Usually this type of work has a linear model and equation in paper
  • But this project had many models and variable levels of performance/appropriateness
  • App gives the user the ability to get predictions with new data and inspect diagnostic plots

shiny maps can make your work more accessible

Resources

Thank you

@Rex_Parsons8

@RWParsons




Slides were made using ioslides_presentation in RMarkdown